package gestion.Controlador;

import gestion.negocio.GestionUsuario;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Salida
 */
@WebServlet("/Salida")
public class Salida extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Salida() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		if (req.getParameter("opcion") != null) {
			String opcion = req.getParameter("opcion");
			opcion.replaceAll(" ", "");
			int opc = Integer.parseInt(opcion);

			switch (opc) {
			case 1:
				ejecutaSale(req, resp);
				break;
				
			case 2:
				ejecutaNuevoUser(req, resp);
				break;
				
			case 3:
				ejecutaListadoUser(req, resp);
				break;
				
			case 4:
				ejecutaMenu(req,resp);
				break;
				
			default:
				break;
			}

		}

	}

	private void ejecutaSale(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		//resp.setHeader("Cache-Control", "no-store");
		//
		//
		HttpSession sesion = req.getSession();
		sesion.invalidate();

		RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");
		rd.forward(req, resp);

	}
	
	
	
	private void ejecutaNuevoUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		
		RequestDispatcher rd = req.getRequestDispatcher("NuevoUsuario.jsp");
		rd.forward(req, resp);
		
	}
	
	
	private void ejecutaListadoUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
	
		RequestDispatcher rd = req.getRequestDispatcher("ListaUsuario.jsp");
		rd.forward(req, resp);
		
	}
	
	
	private void ejecutaMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		
		Collection<gestion.modelo.Usuario> persona=null;
		GestionUsuario ges = new GestionUsuario();
		
		try {
			persona = ges.listar();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	req.setAttribute("listar", persona);
	
		RequestDispatcher rd = req.getRequestDispatcher("Menu.jsp");
		rd.forward(req, resp);
		
		
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
